package com.fingard.dsp.bank.directbank.bocom01;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillTrans;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.*;
import com.fingard.dsp.bank.reqrespobj.elecbilltrans.detail.*;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/10/16.
 */
public class BOCOM01ElecBillTrans extends BOCOM01Base implements IElecBillTrans {
    /**
     * 通用签收 b2e0109
     */
    @Override
    public RetAtsB003BillRespSubmit D_B003_BillResp_Submit(ReqB003BillRespSubmit query) throws Exception {
        RetAtsB003BillRespSubmit retAts = new RetAtsB003BillRespSubmit(query.reqHeader);
        RetAtsB003BillRespSubmitDetail detail = new RetAtsB003BillRespSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420301";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//签约电子票据
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("SignTp", recourseCodeChange(Integer.parseInt(query.detail.respType)));//签收类型
                xmlWriter.writeElementString("SignMk", "1".equals(query.detail.respFlag) ? "SU00" : "SU01");//签收标志
                xmlWriter.writeElementString("AgrmtNb", query.detail.protocolNo);//协议编号
                if (query.detail.respType.equals("6") && query.detail.respFlag.equals("2")) {
                    if (query.detail.unPayCode.equals("99")) {
                        xmlWriter.writeElementString("DshnrrCode", "DC09");//拒绝代码
                    } else if (Integer.parseInt(query.detail.unPayCode) > 0 && Integer.parseInt(query.detail.unPayCode) <= 9) {
                        int unPayCode = Integer.parseInt(query.detail.unPayCode);
                        xmlWriter.writeElementString("DshnrrCode", "DC0" + (unPayCode - 1));//拒绝代码
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = "拒付理由代码错误";
                        WriteBankLogLn("拒付理由代码错误");
                        retAts.writeDetail(detail);
                        retAts.writeEnd();
                        return retAts;
                    }
                } else {
                    xmlWriter.writeElementString("DshnrrCode", "");//拒绝代码
                }
                xmlWriter.writeElementString("Rmrk", query.detail.unPayMemo);//备注
                xmlWriter.writeElementString("Adr", query.detail.guarAddr);//保证人地址
                xmlWriter.writeElementString("ACCPTRCDTRATGS", "");//承兑人信用评级
                xmlWriter.writeElementString("ACCPTRCDTRATGAGCY", "");//承兑人信用评级机构
                xmlWriter.writeElementString("ACCPTRCDTRATGDUEDT", "");//承兑人信用评级到期日
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 出票 b2e0101
     */
    @Override
    public RetAtsB101BillApplySubmit D_B101_BillResp_Submit(ReqB101BillApplySubmit query) throws Exception {
        RetAtsB101BillApplySubmit retAts = new RetAtsB101BillApplySubmit(query.reqHeader);
        RetAtsB101BillApplySubmitDetail detail = new RetAtsB101BillApplySubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420201";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("DrwrAcctId", query.detail.drawAct);//出票人账号
                xmlWriter.writeElementString("Tp", "1".equals(query.detail.billType) ? "AC01" : "AC02");//票据种类
                xmlWriter.writeElementString("IsseAmt", new BigDecimal(query.detail.billAmt).setScale(2).toString());//金额
                xmlWriter.writeElementString("Ccy", "CNY");//币种
                xmlWriter.writeElementString("DueDt", DateHelper.convertD10toD8(query.detail.dueDate.substring(0, 10)));//票据到期日
                xmlWriter.writeElementString("BanEndrsmtMk", "1".equals(query.detail.transFlag) ? "EM00" : "EM01");//是否可转让
                xmlWriter.writeElementString("PyeeBankFlg", "1".equals(query.detail.payeeSameBnk) ? "0" : "1");//收款人银行开户标志
                xmlWriter.writeElementString("PyeeAcctId", query.detail.payeeAct);//收款人账号
                xmlWriter.writeElementString("PyeeNm", query.detail.payeeName);//收款人名称
                xmlWriter.writeElementString("PyeeAcctSvcr", query.detail.payeeBnkCode);//收款人支付行号
                xmlWriter.writeElementString("DrwrCdtRatgs", query.detail.creditLv);//出票人信用等级
                xmlWriter.writeElementString("CdtRatgAgcy", query.detail.gradeAgent);//评级机构
                if (!StringHelper.isNullOrEmpty(query.detail.gradeDueDate)) {
                    xmlWriter.writeElementString("CdtRatgDueDt", DateHelper.convertD10toD8(query.detail.gradeDueDate.substring(0, 10)));
                } else {
                    //评级到期日，--按照接口文档可选送
                    xmlWriter.writeElementString("CdtRatgDueDt", "");
                    WriteBankLogLn("ATS上送字段<GradeDueDate></GradeDueDate>评级到期日为空");
                }
                if ("1".equals(query.detail.accpSameBnk)) {
                    xmlWriter.writeElementString("AccptrBankFlg", "0");//承兑人开户银行标志
                    xmlWriter.writeElementString("AccptrAcctId", query.detail.accpAct);//承兑人账号
                } else if ("2".equals(query.detail.accpSameBnk)) {
                    xmlWriter.writeElementString("AccptrBankFlg", "1");//承兑人开户银行标志
                    xmlWriter.writeElementString("AccptrAcctId", query.detail.accpAct);//承兑人账号
                }
                xmlWriter.writeElementString("AccptrNm", query.detail.accpName);//承兑人名称
                xmlWriter.writeElementString("AccptrAcctSvcr", query.detail.accpBnkCode);//承兑人支付行号
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo);//出票备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
//                        detail.reqSeqID = tmpBkReader.getFirstText("/ap/body/FlwNo");
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 提示承兑 b2e0105
     */
    @Override
    public RetAtsB102BillAccpSubmit D_B102_BillResp_Submit(ReqB102BillAccpSubmit query) throws Exception {
        RetAtsB102BillAccpSubmit retAts = new RetAtsB102BillAccpSubmit(query.reqHeader);
        RetAtsB102BillAccpSubmitDetail detail = new RetAtsB102BillAccpSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420202";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("ActionCode", "07");//操作标志 02撤票 06撤回 07提示承兑 10提示收票
                xmlWriter.writeElementString("TxlCtrCtnb", query.detail.contractNo);//合同号
                xmlWriter.writeElementString("INVCNB", query.detail.invoiceNo);//发票号
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 撤票 b2e0104
     */
    @Override
    public RetAtsB103BillRetSubmit D_B103_BillResp_Submit(ReqB103BillRetSubmit query) throws Exception {
        RetAtsB103BillRetSubmit retAts = new RetAtsB103BillRetSubmit(query.reqHeader);
        RetAtsB103BillRetSubmitDetail detail = new RetAtsB103BillRetSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420202";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("ActionCode", "02");//操作标志 02撤票 06撤回 07提示承兑 10提示收票
                xmlWriter.writeElementString("TxlCtrCtnb", "");//合同号
                xmlWriter.writeElementString("INVCNB", "");//发票号
                xmlWriter.writeElementString("RmrkByPropsr", "");//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);

        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 提示收票 b2e0108
     */
    @Override
    public RetAtsB104BillReceSubmit D_B104_BillResp_Submit(ReqB104BillReceSubmit query) throws Exception {
        RetAtsB104BillReceSubmit retAts = new RetAtsB104BillReceSubmit(query.reqHeader);
        RetAtsB104BillReceSubmitDetail detail = new RetAtsB104BillReceSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420202";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("ActionCode", "10");//操作标志 02撤票 06撤回 07提示承兑 10提示收票
                xmlWriter.writeElementString("TxlCtrCtnb", "");//合同号
                xmlWriter.writeElementString("INVCNB", "");//发票号
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo);//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 背书转让申请 b2e0280
     */
    @Override
    public RetAtsB105BillEndoSubmit D_B105_BillResp_Submit(ReqB105BillEndoSubmit query) throws Exception {
        RetAtsB105BillEndoSubmit retAts = new RetAtsB105BillEndoSubmit(query.reqHeader);
        RetAtsB105BillEndoSubmitDetail detail = new RetAtsB105BillEndoSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420302";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体2
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("BanEndrsmtMk", "1".equals(query.detail.transFlag) ? "EM00" : "EM01");//不得转让标志 EM00不禁止 EM01禁止
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo);//背书人备注
                xmlWriter.writeElementString("EeBankFlg", "1".equals(query.detail.receSameBank) ? "0" : "1");//被背书人开户银行标志
                xmlWriter.writeElementString("EeNm", query.detail.receName);//被背书人名称
                xmlWriter.writeElementString("EeAcctId", query.detail.receAct);//被背书人账号
                xmlWriter.writeElementString("EeAcctSvcr", query.detail.receBnkCode);//被背书人开户行行号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 贴现申请 b2e0110
     */
    @Override
    public RetAtsB106BillDiscSubmit D_B106_BillResp_Submit(ReqB106BillDiscSubmit query) throws Exception {
        RetAtsB106BillDiscSubmit retAts = new RetAtsB106BillDiscSubmit(query.reqHeader);
        RetAtsB106BillDiscSubmitDetail detail = new RetAtsB106BillDiscSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420303";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                String isSameBank = "";
                if ("1".equals(query.detail.receSameBnk)) {
                    isSameBank = "0";
                } else {
                    isSameBank = "1";
                }
                xmlWriter.writeElementString("BankFlg1", isSameBank);//贴入行标志 0交行 1他行 2交行代理财务公司 ??
                xmlWriter.writeElementString("DscntBkNm", query.detail.receBnkName);//贴入行名称
                xmlWriter.writeElementString("DscntBkAcctSvcr", query.detail.receBnkCode);//贴入行行号
                xmlWriter.writeElementString("DscntBkAcctId", query.detail.receAct);//贴入账号
                xmlWriter.writeElementString("BankFlg2", "0");//入帐账号归属行标志??
                xmlWriter.writeElementString("AcctId", query.detail.actedAct);//入账账号
                if ("0".equals(isSameBank) && !"1".equals(query.detail.discType)) {
                    throw new Exception("同行不支持回购式贴现");
                }
                xmlWriter.writeElementString("RpdMk", "1".equals(query.detail.discType) ? "RM00" : "RM01");//贴现种类
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(new Date());
                calendar1.add(Calendar.DAY_OF_MONTH, 1);
                String queryDate = getBankConfig().getValueAsString("queryDate");
                if (StringHelper.hasAnyChar(queryDate)) {
                    xmlWriter.writeElementString("tr_acdt", queryDate);
                } else {
                    xmlWriter.writeElementString("DsAppDt", Format.Date8Format.format(calendar1.getTime()));//贴现日期??
                }
                xmlWriter.writeElementString("IntrstRate", new BigDecimal(query.detail.discRate).multiply(new BigDecimal("0.01")).toString());//贴现利率
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(Format.Date10Format.parse(query.detail.dueDate.substring(0, 10)));
                int diffDay = (int) Math.ceil(DateHelper.getDifferDays2(Format.Date10Format.parse(query.detail.dueDate.substring(0, 10)), new Date()));
                if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
                    diffDay += 2;
                } else if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                    diffDay += 1;
                }
                BigDecimal discAmt = new BigDecimal(query.detail.billAmt).
                        subtract(new BigDecimal(query.detail.billAmt).
                                multiply(new BigDecimal(query.detail.discRate)).
                                multiply(new BigDecimal(diffDay + "")).
                                divide(new BigDecimal(36000 + ""), 4, BigDecimal.ROUND_HALF_UP));
                xmlWriter.writeElementString("Amt", discAmt.setScale(2, BigDecimal.ROUND_HALF_UP).toString());//贴现实付金额
                xmlWriter.writeElementString("BanEndrsmtMk", "1".equals(query.detail.transFlag) ? "EM00" : "EM01");//不得转让标志
                xmlWriter.writeElementString("SttlmMk", "1".equals(query.detail.payType) ? "SM00" : "SM01");//线上清算标志
                if ("2".equals(query.detail.discType)) {
                    xmlWriter.writeElementString("RpdOpenDt", DateHelper.convertD10toD8(query.detail.repOpenDate));//贴现赎回开发日
                    xmlWriter.writeElementString("RpdDueDt", DateHelper.convertD10toD8(query.detail.repDueDate));//贴现赎回截止日
                    xmlWriter.writeElementString("RpdIntrstRate", new BigDecimal(query.detail.repRate).multiply(new BigDecimal("0.01")).toString());//贴现赎回利率
                    xmlWriter.writeElementString("RpdAmt", new BigDecimal(query.detail.repAmt).setScale(2).toString());//贴现赎回金额
                } else {
                    xmlWriter.writeElementString("RpdOpenDt", "");//贴现赎回开发日
                    xmlWriter.writeElementString("RpdDueDt", "");//贴现赎回截止日
                    xmlWriter.writeElementString("RpdIntrstRate", "");//贴现赎回利率
                    xmlWriter.writeElementString("RpdAmt", "");//贴现赎回金额
                }
                xmlWriter.writeElementString("TxlCtrctNb", query.detail.contractNo);//交易合同编号
                xmlWriter.writeElementString("InvcNb", query.detail.invoiceNo);//发票号码
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo);//贴出人备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 质押申请 b2e0285
     */
    @Override
    public RetAtsB107BillPledSubmit D_B107_BillResp_Submit(ReqB107BillPledSubmit query) throws Exception {
        RetAtsB107BillPledSubmit retAts = new RetAtsB107BillPledSubmit(query.reqHeader);
        RetAtsB107BillPledSubmitDetail detail = new RetAtsB107BillPledSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420305";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("OperateFlag", "0");//0质押申请
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DAY_OF_MONTH, 1);
                String queryDate = getBankConfig().getValueAsString("queryDate");
                if (StringHelper.hasAnyChar(queryDate)) {
                    xmlWriter.writeElementString("ApplDt", queryDate);
                } else {
                    xmlWriter.writeElementString("ApplDt", Format.Date8Format.format(calendar.getTime()));//质押申请日??
                }
                xmlWriter.writeElementString("BtchNb", "");//批次号
                xmlWriter.writeElementString("RmrkByPropsr", query.detail.memo);//出质人备注
                xmlWriter.writeElementString("Biztyp", "0");//质押类型??
                xmlWriter.writeElementString("CollztnAcctId", "");//票据池账号
                xmlWriter.writeElementString("PawneeName", query.detail.receBnkName);//质权人名称
                xmlWriter.writeElementString("PawneeAcctSvcr", query.detail.receBnkCode);//质权人开户行行号
                String date = new StringBuilder(Format.Date10Format.format(new Date())).append("T").append(Format.Time8Format.format(new Date())).toString();
                xmlWriter.writeElementString("QryDt", date);//通讯交易标识
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 提示付款 b2e0111
     */
    @Override
    public RetAtsB108BillPaySubmit D_B108_BillResp_Submit(ReqB108BillPaySubmit query) throws Exception {
        RetAtsB108BillPaySubmit retAts = new RetAtsB108BillPaySubmit(query.reqHeader);
        RetAtsB108BillPaySubmitDetail detail = new RetAtsB108BillPaySubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420304";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("SttlmMk", "1".equals(query.detail.payType) ? "SM00" : "SM01");//线上清算标志
                xmlWriter.writeElementString("Rmk", query.detail.memo);//提示付款人备注
                xmlWriter.writeElementString("Rsn", query.detail.overDueRea);//逾期原因
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 保证申请  b2e0281 不支持
     */
    @Override
    public RetAtsB109BillGuarSubmit D_B109_BillResp_Submit(ReqB109BillGuarSubmit query) throws Exception {
        return null;
    }

    /**
     * 追索通知 b2e0282
     */
    @Override
    public RetAtsB110BillRecoSubmit D_B110_BillResp_Submit(ReqB110BillRecoSubmit query) throws Exception {
        RetAtsB110BillRecoSubmit retAts = new RetAtsB110BillRecoSubmit(query.reqHeader);
        RetAtsB110BillRecoSubmitDetail detail = new RetAtsB110BillRecoSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420309";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("RcrsrRole", "RC01");//追索人类别
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码

                xmlWriter.writeElementString("ApplDt", Format.Date8Format.format(new Date()));//追索通知日期
                xmlWriter.writeElementString("ReqAmt", new BigDecimal(query.detail.recourseAmt).setScale(2).toString());//追索金额
                if ("1".equals(query.detail.recourseType)) {
                    xmlWriter.writeElementString("RcrsRsnCd", "");//追索理由代码
                } else {
                    xmlWriter.writeElementString("RcrsRsnCd", "1".equals(query.detail.recourseCode) ? "RC00" : "RC01");//追索理由代码
                }
                xmlWriter.writeElementString("RcrsRmrk", query.detail.memo);//追索通知备注
                xmlWriter.writeElementString("RcvgPrsnOfRcrsAcctId", query.detail.receAct);//清偿人账号
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    /**
     * 同意清偿 b2e0112
     */
    @Override
    public RetAtsB111BillAgreeSubmit D_B111_BillResp_Submit(ReqB111BillAgreeSubmit query) throws Exception {
        //交行直联暂不支持，使用网银清偿
        return null;
    }

    /**
     * 交易撤回 b2e0106
     */
    @Override
    public RetAtsB112BillCancSubmit D_B112_BillResp_Submit(ReqB112BillCancSubmit query) throws Exception {
        RetAtsB112BillCancSubmit retAts = new RetAtsB112BillCancSubmit(query.reqHeader);
        RetAtsB112BillCancSubmitDetail detail = new RetAtsB112BillCancSubmitDetail();
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                tr_code = "420202";//交易码
                req_no = codeToAscii(query.detail.reqSeqID);//企业凭证号
                XmlTextWriter xmlWriter = new XmlTextWriter();
                writeCommonHeader(xmlWriter);
                //拼接请求报文体
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("QryAcct", query.detail.actNum);//账号
                xmlWriter.writeElementString("IdNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("ActionCode", "06");//操作标志 02撤票 06撤回 07提示承兑 10提示收票
                //        xmlWriter.writeElementString("TxlCtrCtnb","");//合同号
                //        xmlWriter.writeElementString("INVCNB", "");//发票号
                //        xmlWriter.writeElementString("RmrkByPropsr","");//备注
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                //请求银行
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder getBankRetInfo = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], getBankRetInfo);
                    if (tmpBkReader != null) {
                        String code = tmpBkReader.getFirstText("/ap/head/ans_code");
                        String info = tmpBkReader.getFirstText("/ap/head/ans_info");
                        if ("0".equals(code)) {
                            detail.respCode = "0";
                            detail.respInfo = tmpBkReader.getFirstText("/ap/head/particular_info");
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.transState = tmpBkReader.getFirstText("/ap/head/particular_code");
                            detail.payInfoCode = detail.transState;
                        } else {
                            detail.respCode = "-1";
                            detail.respInfo = info;
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpBkReader.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpStrRet[0].toString();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                detail.respCode = "-1";
                detail.respInfo = ex.getMessage();
            }
            retAts.writeDetail(detail);
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAtsB113ProtocolSubmit D_B113_ProtocolResp_Submit(ReqB113ProtocolSubmit query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB119BillRelePledSubmit D_B119_BillResp_Submit(ReqB119BillRelePledSubmit query) throws Exception {
        return null;
    }
}
